
import json
import ai_service.ai_service_interface as ai_interface
from openai import OpenAI

class AIServiceKimi(ai_interface.AIServiceParseInterface):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.keys = {}
        with open("config/prompt.json", encoding='utf-8') as file:
            content = file.read()
            if len(content) > 0:
                result = json.loads(content)
                self.prompt = result['prompt']
            file.close()
        
        with open("config/config_kimi.json", encoding='utf-8') as file:
            content = file.read()
            if len(content) > 0:
                result = json.loads(content)
                self.keys = result
            file.close()
        print("url %s " % (self.keys["url"]))
        self.client = OpenAI(
            api_key = self.keys['key'],
            base_url = self.keys['url'],
        )

    def request_text(self, text: str) -> str:
        msg = []

        msg_one = {
            'role': 'system',
            'content': '你是一个开发者，可以轻松的将文字转换成json等数据格式'
        }
        msg.append(msg_one)
        msg_two = {
            'role': 'user',
            'content': text + ', ' + self.prompt
        }
        msg.append(msg_two)
        completion = self.client.chat.completions.create(
            model = 'moonshot-v1-8k',
            messages = msg,
            temperature = 0.2,
        )

        return completion.choices[0].message.content

    def parse_duty(self, text: str) -> str:
        result = self.request_text(text)

        return result
    
def createServiceKimi() -> ai_interface.AIServiceParseInterface:
    service = AIServiceKimi()

    return service